﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ParamSetCollection.General.Parameters;
using ParamSetCollection.General.Parameters.Normal;

namespace ParamSetCollection.General.ParamTable
{
    public class ParamGroupUnit
    {
        public event OnBitValueSetCmdEventHandler OnBitValueSetCmd;
        public event OnValueSetCmdEventHandler OnValueSetCmd;
        public event OnValueChangeLogHandler OnValueChangeLog;

        public Dictionary<string, ParameterUnit> bitParamDic = new Dictionary<string, ParameterUnit>();
        public Dictionary<string, ParameterUnit> shortParamDic = new Dictionary<string, ParameterUnit>();
        public Dictionary<string, ParameterUnit> wordParamDic = new Dictionary<string, ParameterUnit>();
        public Dictionary<string, ParameterUnit> floatParamDic = new Dictionary<string, ParameterUnit>();
        public Dictionary<string, ParameterUnit> dwParamDic = new Dictionary<string, ParameterUnit>();

        public virtual ParameterUnit GetBindingParams(string tag)
        {
            if(bitParamDic.ContainsKey(tag))
            {
                return bitParamDic[tag];
            }
            else if(shortParamDic.ContainsKey(tag))
            {
                return shortParamDic[tag];
            }
            //else if (wordParamDic.ContainsKey(tag))
            //{
            //    return wordParamDic[tag];
            //}
            //else if (floatParamDic.ContainsKey(tag))
            //{
            //    return floatParamDic[tag];
            //}
            else if (dwParamDic.ContainsKey(tag))
            {
                return dwParamDic[tag];
            }
            else
            {
                return null;
            }



        }

        public void BindingCommData(List<CommDataUnit> source)
        {
            List<string> keys = bitParamDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                float f = 0;
                if(float.TryParse(bitParamDic[keys[i]].Position,out f))
                {
                    int pos= (int)f;
                    bitParamDic[keys[i]].InputBindingData(source[pos - CommDataUnit.AddrHead]);
                    bitParamDic[keys[i]].OnBitValueSet += ParamGroupUnit_OnBitValueSet;
                    bitParamDic[keys[i]].OnValueChangeLog += ParamGroupUnit_OnValueChangeLog;
                }
            }

            keys = shortParamDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                int t = 0;
                if (int.TryParse(shortParamDic[keys[i]].Position, out t))
                {
                    int pos = t;
                    shortParamDic[keys[i]].InputBindingData(source[pos - CommDataUnit.AddrHead]);
                    shortParamDic[keys[i]].OnValueSetCmd += ParamGroupUnit_OnValueSetCmd;
                    shortParamDic[keys[i]].OnValueChangeLog += ParamGroupUnit_OnValueChangeLog;
                }
            }
            keys = wordParamDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                int t = 0;
                if (int.TryParse(wordParamDic[keys[i]].Position, out t))
                {
                    int pos = t;
                    wordParamDic[keys[i]].InputBindingData(source[pos - CommDataUnit.AddrHead]);
                    wordParamDic[keys[i]].OnValueSetCmd += ParamGroupUnit_OnValueSetCmd;
                    wordParamDic[keys[i]].OnValueChangeLog += ParamGroupUnit_OnValueChangeLog;
                }
            }

            keys = floatParamDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                int t = 0;
                if (int.TryParse(floatParamDic[keys[i]].Position, out t))
                {
                    int pos = t;
                    floatParamDic[keys[i]].InputBindingData(source[pos - CommDataUnit.AddrHead]);
                    floatParamDic[keys[i]].OnValueSetCmd += ParamGroupUnit_OnValueSetCmd;
                    floatParamDic[keys[i]].OnValueChangeLog += ParamGroupUnit_OnValueChangeLog;
                }
            }

            keys = dwParamDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                int t = 0;
                if (int.TryParse(dwParamDic[keys[i]].Position, out t))
                {
                    int pos = t;
                    dwParamDic[keys[i]].InputBindingData(source[pos - CommDataUnit.AddrHead], source[pos - CommDataUnit.AddrHead + 1]);
                    dwParamDic[keys[i]].OnValueSetCmd += ParamGroupUnit_OnValueSetCmd;
                    dwParamDic[keys[i]].OnValueChangeLog += ParamGroupUnit_OnValueChangeLog;
                }
            }

        }

        private void ParamGroupUnit_OnValueChangeLog(Logs.ParamRenewLogMsgUnit logUnit)
        {
            OnValueChangeLog?.Invoke(logUnit);
        }

        protected void InitParams(Dictionary<string, ParameterUnit> paramsDic)
        {
            List<string> keys = paramsDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                paramsDic[keys[i]] = new ParameterUnit(keys[i]);
            }
        }

        protected void InitParamsType(Dictionary<string, ParameterUnit> paramsDic,Type type,string idcType)
        {
            List<string> keys = paramsDic.Keys.ToList();
            for (int i = 0; i < keys.Count; i++)
            {
                paramsDic[keys[i]].ParamType = type;
            }

        }


        private void ParamGroupUnit_OnBitValueSet(int mainAddr, int bitIndex, byte[] baseValue, bool setValue, string idcType)
        {
            OnBitValueSetCmd?.Invoke(mainAddr, bitIndex, baseValue, setValue,idcType);
        }

        private void ParamGroupUnit_OnValueSetCmd(int addr, byte[] data, string idcType)
        {
            OnValueSetCmd?.Invoke(addr, data,idcType);
        }

    }
}
